package com.android.demo;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.ActivityGroup;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Window;
import android.widget.LinearLayout;

public class TestCompressBitmap extends ActivityGroup {

	/** Called when the activity is first created. */
	private final static String  TAG = "TestCompressBitmap";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(new LinearLayout(this));

		testSaveBitmap();
	}
	
	public int getDegree(String filename){
		int result = 0; 
		int orientation = 0;
		try {
			ExifInterface exif = new ExifInterface(filename);	
			orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
			
		}catch (Exception e) {
		}
		
		switch (orientation) {
		case ExifInterface.ORIENTATION_ROTATE_90:
			result = 90;
			break;
		case ExifInterface.ORIENTATION_ROTATE_180:
			result = 180;
			break;
		case ExifInterface.ORIENTATION_ROTATE_270:
			result = 270;
			break;
		}
		
		return result;
	}
	
	public void testSaveBitmap(){
		byte[] byteImage = getByteArrayFromAsserts("image_rotated.jpg");
		saveByte(byteImage, "/testCompressBitmap", "/image.jpg");
		
		//1 get bitmap
		Bitmap bitmap = ByteArrayToBitmap(byteImage);
		//2 get exif and rotate
		bitmap = compress(bitmap, 480, getDegree(Environment.getExternalStorageDirectory().getPath()+"/testCompressBitmap/image.jpg"));
		//3 get bytes
		byteImage = BitmapToByteArray(bitmap);
		//4 save bytes
		saveByte(byteImage, "/testCompressBitmap", "/image_compressed.jpg");
	}
	
	//1 get byte from asserts
	public byte[] getByteArrayFromAsserts(String fileName){
		InputStream inputStream = null;
		byte[] result = null;
		try {
			inputStream = getAssets().open(fileName);
			
			int size = inputStream.available();
			result = new byte[size];
			
			inputStream.read(result);
			
		}catch (IOException e) {
			Log.i(TAG, "getAssertFromAsserts" + e.toString());
		}
		
		finally{
			try {
				if (inputStream!=null){
					inputStream.close();
				}
			}catch(IOException e) {
				Log.i(TAG, "getAssertFromAsserts" + e.toString());
			}
		}
		
		return result;
	}
	
	//2 compress bitmap
	public Bitmap compress(Bitmap bitmap, int size , int degree) {
		// create explicit picture
		int max = bitmap.getWidth()>bitmap.getHeight()?bitmap.getWidth():bitmap.getHeight();
		int min = bitmap.getWidth()<bitmap.getHeight()?bitmap.getWidth():bitmap.getHeight();
		min = size*min/max;
		max = size;
		
		
		
		if (bitmap.getWidth()>bitmap.getHeight()){
			bitmap = Bitmap.createScaledBitmap(bitmap, max, min, true);
		}else {
			bitmap = Bitmap.createScaledBitmap(bitmap, min, max, true);
		}
		
		Matrix matrix = new Matrix();
		matrix.postRotate(degree);
		bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
		
		return bitmap;
	}
	
	//3 save byte to sdcard
	public static boolean saveByte(byte [] byteArray, String dir , String name) {
		String rootDir = Environment.getExternalStorageDirectory().getPath();//+"/momo/";
		
		File dirFile = new File(rootDir+dir);
		if (!dirFile.exists()) {
			dirFile.mkdirs();
		}
		
		File myCaptureFile = new File(rootDir+dir+name);
		
		FileOutputStream fileOutPutStream;
		try {
			
			fileOutPutStream = new FileOutputStream(myCaptureFile);
			
			fileOutPutStream.write(byteArray, 0, byteArray.length);
		} catch (Exception e) {
			Log.e(TAG, "saveBitmap" + e.toString());
			return false;
		}
		
		try{
			fileOutPutStream.close();
		}catch (Exception e) {}
		
		return true;
	}
	
	public byte[] BitmapToByteArray(Bitmap bitmap ){
		byte[] result = null;
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		bitmap.compress(CompressFormat.JPEG, 80, stream);
		result = stream.toByteArray();
		
		return result;
	}
	
	public Bitmap ByteArrayToBitmap(byte[] byteArray) {
		Bitmap result = null;
		BitmapFactory.Options option;
		
		result = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
		return result;
	}
}
